﻿using System;

namespace Zinnia.Lite.Entity
{
    public class ZinniaObject : IZinniaLiteIdentity, IZinnia
    {
        public string Id { get; set; }
    }

    public class ZinniaBaseLite : ZinniaObject, IZinniaBaseLite
    {
        private readonly Type _declareType;
        private DateTime _createTime;
        private DateTime _currentTime;
        private DateTime _modifyTime;

        public ZinniaBaseLite()
        {
            _declareType = this.GetType();
            Id = Guid.NewGuid().ToString().Replace("-", "");
            _currentTime = DateTime.Now;
            _createTime = _currentTime;
        }
        /// <summary>
        ///被赋值之后，创建时间将不在更改
        /// </summary>
        private bool IsNew => _currentTime == _createTime;

        public DateTime CreateTime
        {
            get
            {
                return _createTime;
            }
            set
            {
                if (IsNew)
                {
                    _createTime = value;
                }
            }
        }

        public DateTime ModifyTime
        {
            get { return _modifyTime == DateTime.MinValue ? DateTime.Now : _modifyTime; }
            set { _modifyTime = value; }
        }

        public virtual string GetTableName()
        {
            return _declareType.FullName.Substring(_declareType.FullName.LastIndexOf('.') + 1);
        }
        public virtual string GetSchemaName()
        {
            return "dbo";
        }
    }
}
